import 'package:intl/intl.dart';

class WorkHistory {
  String id;
  String userId;
  String place;
  String craft;
  DateTime startTime;
  DateTime endTime;

  WorkHistory({
    this.id,
    this.userId,
    this.place,
    this.craft,
    this.startTime,
    this.endTime,
  });

  static WorkHistory fromJson(Map<String, dynamic> json) {
    return WorkHistory(
      id: json['id'],
      userId: json['userId'],
      place: json['place'],
      craft: json['craft'],
      startTime: json['startTime'] == null ? null : DateTime.parse(json['startTime']),
      endTime: json['endTime'] == null ? null : DateTime.parse(json['endTime']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': this.id,
      'userId': this.userId,
      'place': this.place,
      'craft': this.craft,
      'startTimeStr': formatDate(this.startTime),
      'endTimeStr': formatDate(this.endTime),
    };
  }

  String formatDate(DateTime dateTime) {
    if (dateTime == null) return null;
    var format = new DateFormat('yyyy-MM-dd');
    return format.format(dateTime);
  }
}
